<!--
* description: 生成拣货任务
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      v-if="initTemplate"
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicApiConfig="apiConfig"
      :dynamicTableCols="tableCols"
      :dynamicFormFields="formField"
      :dynamicUnFormFields="unFormField"
      :dynamicIsShowSelect="true"
      :dynamicIsColumnDrop="true"
      :callbackObj="callbackObj"
    >
      <template slot="PICK_QTY" slot-scope="scope">
        <el-input
          v-model="scope.row.PICK_QTY"
          type="text"
          @input="numChange(scope, 'PICK_QTY')"
          style="width:100%;"
          class="tableTemplate"
        ></el-input>
      </template>
      <template slot="LACK_QTY" slot-scope="scope">
        <el-input
          v-model="scope.row.LACK_QTY"
          type="text"
          @input="numChange(scope,'LACK_QTY')"
          style="width:100%;"
          class="tableTemplate"
        ></el-input>
      </template>
    </one-table-template>
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import { wmsApis } from "@/api/wms/outStock/api";
import OneTableTemplate from "@/components/wms/templates/oneTable";
import request from "@/utils/request";
export default {
  name: "orderShortage",
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],
  components: {
    OneTableTemplate
  },
  watch: {
    $route(val) {
      if (
        this.formField.pageId !== "" &&
        this.$route.query.pageId !== undefined &&
        this.$route.query.pageId !== this.formField.pageId
      ) {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
        this.queryTable();
      }
    }
  },
  created() {
    if (this.$route.query != null) {
      // 判断传入query参数，初始化数据
      if (this.$route.query.pageId != null && this.$route.query.pageId !== "") {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
      }
    }
  },
  mounted() {
    this.querySelect();
  },
  data() {
    return {
      // 网格查询API配置对象
      apiConfig: wmsApis.queryPickTask,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.queryTable(),
          name: "search",
          position: "right",
          text: "查询"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: "重置"
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          clickEvent: () => this.shortage(),
          name: "confirm",
          position: "left",
          text: "短缺拣货"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "compKey1",
          labelName: "仓库名称",
          codeField: "WAREHOUSE_IDS",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: true,
          options: []
        },
        {
          compKey: "compKey2",
          labelName: "主机厂订单号",
          codeField: "SOURCE_ORDER_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey3",
          labelName: "主机厂订单行号",
          codeField: "SO_LINE_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey4",
          labelName: "专营店订单号",
          codeField: "PURCHASE_ORDER_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey5",
          labelName: "出库去向名称",
          codeField: "PARTNER_SHORT_NAME",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey6",
          labelName: "备件编号",
          codeField: "PART_NO",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey7",
          labelName: "领用人",
          codeField: "HOLD_USER",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey8",
          labelName: "备货单号",
          codeField: "STOCK_UP_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey9",
          labelName: "备货状态",
          codeField: "STOCK_UP_STATUS",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: []
        },
        {
          compKey: "compKey10",
          labelName: "订单接收日期",
          codeField: "ORDER_TIME_START",
          component: () => import("@/components/wms/datePicker/andtDatePicker"),
          type: "date",
          isMust: true
        }
      ],
      // 动态生成网格列
      tableCols: [
        {
          prop: "PICK_CODE",
          label: "拣货单号",
          width: "140px",
          align: "center"
        },
        {
          prop: "PICK_STATUS_NAME",
          label: "拣货状态",
          width: "140px",
          align: "center"
        },
        {
          prop: "DELIVER_CODE",
          label: "发货单号",
          width: "140px",
          align: "center"
        },
        {
          prop: "HOLD_USER_NAME",
          label: "领用人",
          width: "140px",
          align: "center"
        },
        {
          prop: "HOLD_TIME_NEW",
          label: "领用时间",
          width: "140px",
          align: "center"
        },
        {
          prop: "WAREHOUSE_NAME",
          label: "仓库名称",
          width: "140px",
          align: "center"
        },
        {
          prop: "SOURCE_ORDER_CODE",
          label: "主机厂订单号",
          width: "140px",
          align: "center"
        },
        {
          prop: "SO_LINE_CODE",
          label: "主机厂订单行号",
          width: "140px",
          align: "center"
        },
        {
          prop: "OUT_STORAGE_CODE",
          label: "出库去向编码",
          width: "140px",
          align: "center"
        },
        {
          prop: "PARTNER_SHORT_NAME",
          label: "出库去向名称",
          width: "140px",
          align: "center"
        },
        {
          prop: "PURCHASE_ORDER_CODE",
          label: "专营店订单号",
          width: "140px",
          align: "center"
        },
        {
          prop: "ORDER_TYPE_NAME",
          label: "订单类型",
          width: "140px",
          align: "center"
        },
        {
          prop: "STOCK_UP_CODE",
          label: "备货单号",
          width: "140px",
          align: "center"
        },
        {
          prop: "ORDER_TIME_NEW",
          label: "订单接收日期",
          width: "140px",
          align: "center"
        },
        {
          prop: "PART_NO",
          label: "备件编号",
          width: "140px",
          align: "center"
        },
        {
          prop: "PART_NAME",
          label: "备件名称",
          width: "140px",
          align: "center"
        },
        {
          prop: "REQUESTED_QTY",
          label: "要求发货数量",
          width: "140px",
          align: "center"
        },
        {
          prop: "STOCK_UP_STATUS_NAME",
          label: "备货状态",
          width: "140px",
          align: "center"
        },
        {
          prop: "PICK_QTY",
          label: "拣货数量+",
          width: "120px",
          align: "center",
          isSlot: true,
          overflow: false
        },
        {
          prop: "LACK_QTY",
          label: "短缺数量+",
          width: "120px",
          align: "center",
          isSlot: true,
          overflow: false
        }
      ],
      //表单查询数据
      formField: {
        WAREHOUSE_IDS: "",
        SOURCE_ORDER_CODE: "",
        SO_LINE_CODE: "",
        PURCHASE_ORDER_CODE: "",
        PARTNER_SHORT_NAME: "",
        PART_NO: "",
        STOCK_UP_CODE: "",
        HOLD_USER: "",
        STOCK_UP_STATUS: "",
        ORDER_TIME_START: "",
        WAREHOUSE_IDS_NEW: "",
        PICK_STATUS: "20"
      },
      unFormField: {},
      // 回调函数
      callbackObj: {
        //  当选择项发生变化时会触发该事件
        onCheckSelectChange: selection => {
          this.checkTableList = selection;
        }
      },
      // 选中的表格数据
      checkTableList: [],
      // 控制初始化页面
      initTemplate: false
    };
  },
  methods: {
    numChange(scope, str) {
      scope.row[str] = (scope.row[str] + "").replace(/[^\d]/g, "");
      if (scope.row[str] < 1) {
        scope.row[str] = 0;
      }
      if (scope.row[str][0] === "0" && scope.row[str].length > 1) {
        scope.row[str] = scope.row[str].substr(1, scope.row[str].length - 1);
      }
      if (str === "PICK_QTY") {
        if (scope.row[str] > scope.row.REQUESTED_QTY) {
          scope.row[str] = scope.row.REQUESTED_QTY;
          scope.row.LACK_QTY = 0;
        } else {
          scope.row.LACK_QTY = scope.row.REQUESTED_QTY - (scope.row[str] - 0);
        }
      } else if (str === "LACK_QTY") {
        if (scope.row[str] > scope.row.REQUESTED_QTY) {
          scope.row[str] = scope.row.REQUESTED_QTY;
          scope.row.PICK_QTY = 0;
        } else {
          scope.row.PICK_QTY = scope.row.REQUESTED_QTY - (scope.row[str] - 0);
        }
      }
    },
    shortage() {
      if (this.checkTableList.length > 0) {
        this.$confirm("确定要短缺拣货吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        })
          .then(() => {
            request({
              url: "/mp/wm/vl/warehouse/outstoragetask/lackPick.do",
              method: "post",
              data: {
                LackPick: this.checkTableList
              }
            }).then(res => {
              if (res.result === "1") {
                this.queryTable();
                this.$message({
                  message: "短缺拣货成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: res.debugMsg ? res.debugMsg : res.msg,
                  type: "warn"
                });
              }
            });
          })
          .catch(err => {
            console.info(err);
          });
      } else {
        this.$message({
          message: "请选择一条数据"
        });
      }
    },
    querySelect() {
      request({
        url: "/mp/wm/vl/basedata/warehouseuser/queryUserByWHBox.do",
        method: "post",
        data: {
          IS_ENABLE: "1"
        }
      }).then(res => {
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.map(item => {
            this.formField.WAREHOUSE_IDS_NEW += item.WAREHOUSE_ID + ",";
            this.tableComponents[0].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          });
          this.formField.WAREHOUSE_IDS_NEW = this.formField.WAREHOUSE_IDS_NEW.substr(
            0,
            this.formField.WAREHOUSE_IDS_NEW.length - 1
          );
          this.formField.WAREHOUSE_IDS = res.listRow[0].WAREHOUSE_ID;
        }
        this.initTemplate = true;
        this.$nextTick(() => {
          this.queryTable();
        });
      });

      request({
        url: "/mp/wm/vl/warehouse/outstoragetask/queryStockUpStatusBox.do",
        method: "post",
        data: {}
      }).then(res => {
        res.listRow.map(item => {
          this.tableComponents[8].options.push({
            label: item.STOCK_UP_STATUS_NAME,
            value: item.STOCK_UP_STATUS
          });
        });
      });
    }
  }
};
</script>
<style type="text/css" lang="scss" scoped>
/deep/ .tableTemplate .el-input__inner {
  border: none;
}
</style>